package com.atbeijing.bigdata.spark.sql

import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession

object SparkSQL06_UDF {

    def main(args: Array[String]): Unit = {

        // TODO 创建环境对象
        val sparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkSQL")
        val spark: SparkSession = SparkSession.builder().config(sparkConf).getOrCreate()
        import spark.implicits._

        // RDD
        val rdd = spark.sparkContext.makeRDD(
            List(
                (1, "zhangsan", 30),
                (2, "lisi", 40),
                (3, "wangwu", 50)
            )
        )

        val df = rdd.toDF("id", "name", "age")

        df.createOrReplaceTempView("user")

        // 将查询结果的每一条数据经过UDF函数处理，返回处理后的结果
        // 需要将自定义的函数注册到Spark中，这样Spark才能在SQL中识别
        spark.udf.register("headUpper", (name:String)=>{
            name.head.toUpper + name.tail
        })

        // sql => spark => rdd

        spark.sql("select headUpper(name) from user").show


        spark.stop()
    }
}
